//
//  HYLNewFeatureViewController.swift
//  iOSwechat
//
//  Created by apple on 16/2/29.
//  Copyright © 2016年 apple. All rights reserved.
//

import UIKit

private let reuseIdentifier = "reuseIdentifier"
private let ItemCount = 4

class HYLNewFeatureViewController: UICollectionViewController {

    // 布局属性
    var layout : UICollectionViewFlowLayout = UICollectionViewFlowLayout()
    
    init() {
        super.init(collectionViewLayout:layout)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
//    lazy var backgroundImageView = UIImageView()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // 注册
        self.collectionView?.registerClass(HYLNewFeatureCell.self, forCellWithReuseIdentifier: reuseIdentifier)
   
        layout.itemSize = UIScreen.mainScreen().bounds.size
        layout.scrollDirection = UICollectionViewScrollDirection.Horizontal
        layout.minimumLineSpacing = 0
        
        collectionView?.pagingEnabled = true
        collectionView?.bounces = false
        
    }
}

// MARK: - CollectionView
extension HYLNewFeatureViewController {
    // 返回cell 数量
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return ItemCount
    }
    
    // 返回cell 内容
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! HYLNewFeatureCell
        // 在这个地方做动画不合适,这里是cell即将显示就调用了.
        // cell.scaleAnimation()
        cell.itemIndex = indexPath.item
        return cell
    }
    
    override func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
        // 获取当前显示的是哪一页
        // scrollView实际上是collectionView
        let index = Int(scrollView.contentOffset.x / scrollView.bounds.size.width)
        // 判断是否是最后一页
        if index == ItemCount - 1 {
            // 获取cell,在调用cell的动画
            let visibleIndexPath = NSIndexPath(forItem: index, inSection: 0)
            if let cell = collectionView?.cellForItemAtIndexPath(visibleIndexPath) as? HYLNewFeatureCell {
                cell.scaleAnimation()
            }
  
    }
}

// 自定义的cell
class HYLNewFeatureCell : UICollectionViewCell{
    
    var itemIndex: Int = 0 {
        didSet{
            bgView.image = UIImage(named: "new_feature_\(itemIndex + 1)")
            startButton.hidden = true
        }
    }
    
    // 构造函数
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        prepareUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    func prepareUI() {
        // 添加控件
        contentView.addSubview(bgView)
        contentView.addSubview(startButton)
        // 设置约束

        // 背景图片
        bgView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(UIEdgeInsetsZero)
        }
        
        // 体验按钮
        startButton.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(contentView)
            make.bottom.equalTo(contentView).offset(-140)
        }
        
        
    }
    
    // MARK: - 点击开始体验
    private func scaleAnimation() {
        // 显示按钮
        startButton.hidden = false
        
        // 先设置最小
        startButton.transform = CGAffineTransformMakeScale(0, 0)
        
        UIView .animateWithDuration(1.2, delay: 0.1, usingSpringWithDamping: 0.5, initialSpringVelocity: 5, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            // 改变transform
            // startButton.transform = CGAffineTransformMakeScale(1, 1)
            // 还原默认的transform
                self.startButton.transform = CGAffineTransformIdentity
            }) { (_) -> Void in
                
        }
    }
    
    // MARK: - 按钮点击事件
    func startButtonClick() {
        // 跳转到MainViewController
        //        let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
        //        appDelegate.switchRootViewController(true)
        AppDelegate.outswitchViewController(true)
    }

    
    // 开始体验按钮
    lazy var startButton: UIButton = {
        let button = UIButton()
        
        // 设置背景图片
        button .setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: UIControlState.Normal)
        button .setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: UIControlState.Highlighted)
        // 设置文字
        button .setTitle("开始体验", forState:  UIControlState.Normal)
        button .sizeToFit()
        
        // 添加点击事件
        button.addTarget(self, action: "startButtonClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
    }()
    
    lazy var bgView: UIImageView = UIImageView (image: UIImage(named: "new_feature_1"))
    
}
    
}


